package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.constants.CertificationConstants;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author lizhijun
 * @description 针对表【worker_certification_audit(服务人员认证审核表)】的数据库操作Service实现
 * @createDate 2025-09-26 15:59:57
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit>
        implements IWorkerCertificationAuditService {

    @Resource
    private IWorkerCertificationService workerCertificationService;
    /**
     * 分页查询认证审核
     *
     * @param pageQueryReqDTO 查询条件
     * @return 分页结果
     */
    @Override
    public Page<WorkerCertificationAudit> pageAudit(WorkerCertificationAuditPageQueryReqDTO pageQueryReqDTO) {
        Page<WorkerCertificationAudit> page = new Page<>(pageQueryReqDTO.getPageNo(), pageQueryReqDTO.getPageSize());
        LambdaQueryWrapper<WorkerCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(pageQueryReqDTO.getAuditStatus() != null, WorkerCertificationAudit::getAuditStatus, pageQueryReqDTO.getAuditStatus());
        queryWrapper.eq(pageQueryReqDTO.getCertificationStatus() != null, WorkerCertificationAudit::getCertificationStatus, pageQueryReqDTO.getCertificationStatus());
        queryWrapper.eq(StringUtils.hasText(pageQueryReqDTO.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, pageQueryReqDTO.getIdCardNo());
        queryWrapper.like(StringUtils.hasText(pageQueryReqDTO.getName()), WorkerCertificationAudit::getName, pageQueryReqDTO.getName());
        return super.page(page, queryWrapper);
    }

    @Override
    public RejectReasonResDTO getRejectReason() {
        LambdaQueryWrapper<WorkerCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkerCertificationAudit::getCertificationStatus, CertificationConstants.CERTIFICATION_STATUS_FAIL)
                .eq(WorkerCertificationAudit::getServeProviderId, UserContext.currentUserId());
        WorkerCertificationAudit workerCertificationAudit = super.getOne(queryWrapper);
        if (workerCertificationAudit == null){
            return null;
        }
        return new RejectReasonResDTO(workerCertificationAudit.getRejectReason());
    }

    @Override
    @Transactional
    public void audit(Long id, Integer certificationStatus, String rejectReason) {

        WorkerCertificationAudit workerCertificationAudit = super.getById(id);
        if (workerCertificationAudit == null){
            throw new RuntimeException("认证审核不存在");
        }
        super.update(Wrappers.< WorkerCertificationAudit>lambdaUpdate()
                .set(WorkerCertificationAudit::getCertificationStatus,certificationStatus)
                .set(WorkerCertificationAudit::getRejectReason,rejectReason)
                .eq(WorkerCertificationAudit::getId,id));
        //更新服务人员认证表
        WorkerCertification workerCertification = new WorkerCertification();
        workerCertification.setId(workerCertificationAudit.getServeProviderId());
        workerCertification.setCertificationStatus(certificationStatus);
        workerCertificationService.updateById(workerCertification);

    }
}




